﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using PPAP.Model;

namespace PPAP.Web.Master
{
    public partial class MailTemplate : PageBase
    {
        MailTemplateBO mbo = new MailTemplateBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    PPAP.Model.MailTemplate mail = mbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                    txtTitle.Text = mail.TITLE;
                    txtName.Text = mail.NAME;
                    rbNo.Checked = mail.ISUSE == 0 ? true : false;
                    rbYes.Checked = mail.ISUSE == 1 ? true : false;
                    content1.InnerText = mail.CONTENT;
                    txtOpportunity.Text = mail.Opportunity;
                    txtRecipients.Text = mail.Recipients;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int num=0;
            PPAP.Model.MailTemplate mail =null;
            if (Request.QueryString["id"] != null)
                mail = mbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
            else
                mail = new Model.MailTemplate();
            mail.TITLE = CheckWord(txtTitle.Text);
            mail.NAME = CheckWord(txtName.Text);
            mail.ISUSE = rbYes.Checked?1:0;
            mail.CREATETIME = DateTime.Now;
            mail.CREATEDBY = CurrentUser.Id;
            mail.CONTENT = CheckWord(content1.InnerText);
            mail.Recipients = CheckWord(txtRecipients.Text);
            mail.Opportunity = CheckWord(txtOpportunity.Text);
            if (Request.QueryString["id"] == null)
                num = mbo.Add(mail);
            else
                num = mbo.Update(mail);
            if (num > 0)
            {
                lbo.WriteLog(CurrentUser.Id, "Edit MailTemplate");
                Response.Redirect("MailTemplateList.aspx");
            }
        }
    }
}